﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace HQWeb.Resources.MultiMaterialList
{
    public partial class SelectTree : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //VerdictIsLogin();
            if (!IsPostBack)
            {
                CheckPara();
                InitTree();
            }
        }

        #region 参数判断
        private void CheckPara()
        {
            if (Request["itemIDValue"] != "")
            {
                //this.txtitemIDValueHid.Value = getItemIDValue(Request["itemIDValue"]);
                this.txtitemIDValueHid.Value = Request["itemIDValue"];
                this.txtitemNameValueHid.Value = getItemNameValue(this.txtitemIDValueHid.Value.Trim());
            }
            if (!string.IsNullOrEmpty(Request["sign"]))
            {
                this.txtSignHid.Value = Convert.ToString(Request["sign"]);
            }
            else
            {
                this.txtSignHid.Value = "0";
            }
        }
        #endregion

        #region 元素类型树
        private void InitTree()
        {
            TreeNode node = new TreeNode();
            node.Text = "元素分类";
            node.Value = "0";
            node.Target = "List";
            node.Expanded = true;
            node.SelectAction = TreeNodeSelectAction.Expand;
            node.NavigateUrl = "SelectList.aspx?objTreeID=0&itemIDValue = " + this.txtitemIDValueHid.Value;
            IList TreeList = new ArrayList();
            BLL.SysManage.T_PUB_MATERIALTYPE MaterialBll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            TreeList = MaterialBll.GetModelList("ISUSED='1'");
            AddReplies(node, TreeList);
            LeftTree.Nodes.Add(node);
        }
        #endregion

        #region 加载元素类型下元素节点
        private void AddReplies(TreeNode node, IList TreeList)
        {
            try
            {
                string strPid = node.Value;
                foreach (Model.SysManage.T_PUB_MATERIALTYPE Material in TreeList)
                {
                    if (Material.PARENT_ID.ToString() == strPid)
                    {
                        TreeNode replyNode = new TreeNode();
                        replyNode.Text = Material.TYPE_NAME.Trim();
                        replyNode.Value = Material.TYPE_ID.ToString();
                        replyNode.NavigateUrl = "SelectList.aspx?objTreeID=" + replyNode.Value + "&itemIDValue=" + this.txtitemIDValueHid.Value;
                        replyNode.Target = "List";
                        replyNode.Expanded = false;
                        replyNode.SelectAction = TreeNodeSelectAction.Expand;
                        node.ChildNodes.Add(replyNode);
                        AddReplies(replyNode, TreeList);
                    }

                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 方法列表
        #region 取得选择对象ID组（按工号排序）
        //private string getItemIDValue(string strIDs)
        //{
        //    string strOIDs = "";
        //    if (strIDs.Length > 0)
        //    {
        //        int intTempIndex = strIDs.IndexOf(',');
        //        if (intTempIndex == 0)
        //        {
        //            strIDs = strIDs.Substring(1, strIDs.Length - 2);
        //        }
        //        string strWhere = "  ID in (" + strIDs + ") order by userNum";
        //        BLL.SysManage.T_PUB_MATERIAL objBll = new BLL.SysManage.T_PUB_MATERIAL();
        //        IList ils = objBll.GetModelList(strWhere);
        //        for (int i = 0; i < ils.Count; i++)
        //        {
        //            COM.HS.Model.SYSTEM_USER_SQL objModel = (COM.HS.Model.SYSTEM_USER_SQL)ils[i];
        //            if (i == ils.Count - 1)
        //            {
        //                strOIDs += Convert.ToString(objModel.ID);
        //            }
        //            else
        //            {
        //                strOIDs += Convert.ToString(objModel.ID) + ",";
        //            }
        //        }
        //    }
        //    return strOIDs;
        //}
        #endregion

        #region 取得员工姓名组
        private string getItemNameValue(string strIDs)
        {
            string strNames = "";
            string strWhere = " MATERIAL_ID in (" + strIDs + ") ";
            BLL.SysManage.T_PUB_MATERIAL objBll = new BLL.SysManage.T_PUB_MATERIAL();
            List<Model.SysManage.T_PUB_MATERIAL> ils = objBll.GetModelList(strWhere);
            for (int i = 0; i < ils.Count; i++)
            {
                if (i == ils.Count - 1)
                {
                    strNames += ils[i].MATERIAL_NAME;
                }
                else
                {
                    strNames += ils[i].MATERIAL_NAME + ",";
                }
            }

            return strNames;
        }
        #endregion
      
        #endregion
    }
}